package edu.cmu.abi;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import edu.cmu.abi.track.Track;

public class Artist {

    private String name;
    private String artistID;
    private ArrayList<Track> songList;

    public String getArtistName() {
        return name;
    }

    public void setName(String s) {
        name = s;
    }

    public void addSong(Track t) {
        songList.add(t);
    }

    public ArrayList<Track> getListOfSongs() {
        return songList;
    }

    public void setListOfSongs(ArrayList<Track> s) {
        songList = s;
    }

    public void read(String name, Connection con) throws SQLException {

        PreparedStatement statement = null;

        try {

            statement = con.prepareStatement("select artist_id, artist_name, track_id, title "
                    + "from songs where UPPER(artist_name)= UPPER(?)");

            statement.setString(1, name);

            ResultSet result = statement.executeQuery();

            int counter = 0;
            if (result.next()) {
                if (counter == 0) {
                    this.name = name;
                    this.artistID = result.getString(1);
                }
                Track track = new Track(result.getString(3));
                track.setTitle(result.getString(4));
                track.loadCharacteristics();

                this.addSong(track);
                counter++;

            } else {

                System.out.println("Could not read the artist name, please enter a correct artist name");

            }

        } catch (Exception e) {
            System.out.println("Problem with read");

        } finally {

            if (statement != null) {
                statement.close();

            }
        }

    }

    public static ArrayList<Artist> readSimilarArtists(String name, Connection con) throws SQLException {

        PreparedStatement statement = null;
        ArrayList<Artist> artistList = new ArrayList<Artist>();

        try {

            statement = con.prepareStatement("select artist_id, artist_name, track_id, title "
                    + "from songs where artist_id in "
                    + "(select distinct similar from similarity "
                    + "where target = (select distinct artist_id "
                    + "from songs where UPPER(artist_name)= UPPER(?) "
                    + "and rownum<2)) order by artist_id");

            statement.setString(1, name);

            ResultSet result = statement.executeQuery();

            if (result.next()) {
                Track track = new Track(result.getString(3));
                track.setTitle(result.getString(4));
                track.loadCharacteristics();
                HashSet<Artist> artists = new HashSet<Artist>();

                Artist artist = new Artist();
                artist.name = name;
                artist.artistID = result.getString(1);

                //if it is a new artist
                if (artists.add(artist)) {
                    artistList.add(artist);
                }

                //since the resultset returned in order by artist, we'll always add
                //the track to the artist at the end of our list

                artistList.get(artistList.size()).addSong(track);

            } else {

                System.out.println("Could not read the artist name, please enter a correct artist name");
            }

        } catch (Exception e) {
            System.out.println("Problem with read");

        } finally {

            if (statement != null) {
                statement.close();

            }
        }

        return artistList;

    }
}
